<?php
	
	//Adds filter to customize messages for the slide post type

	add_filter( 'post_updated_messages', 'sl_updated_messages' );

	function sl_updated_messages( $messages ) {

		global $post, $post_ID;

		$messages['slide'] = array( 
  
			0  => '',
			1  => sprintf( __( 'Slide updated. <a href="%s">View slide</a>', hybrid_get_textdomain() ), esc_url( get_permalink($post_ID) ) ),
			2  => __( 'Custom field updated.', hybrid_get_textdomain() ),
			3  => __( 'Custom field deleted.', hybrid_get_textdomain() ),
			4  => __( 'Slide updated.', hybrid_get_textdomain() ),
			5  => isset($_GET['revision']) ? sprintf( __( 'Slide restored to revision from %s', hybrid_get_textdomain() ), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
			6  => sprintf( __( 'Slide published. <a href="%s">View slide</a>', hybrid_get_textdomain() ), esc_url( get_permalink($post_ID) ) ),
			7  => __( 'Slide saved.', hybrid_get_textdomain() ),
			8  => sprintf( __( 'Slide submitted. <a target="_blank" href="%s">Preview slide</a>', hybrid_get_textdomain() ), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
			9  => sprintf( __( 'Slide scheduled for: <strong>%1$s</strong>. <a target="_blank" href="%2$s">Preview slide</a>', hybrid_get_textdomain() ), date_i18n( __( 'M j, Y @ G:i', hybrid_get_textdomain() ), strtotime( $post->post_date ) ), esc_url( get_permalink($post_ID) ) ),
			10 => sprintf( __( 'Slide draft updated. <a target="_blank" href="%s">Preview slide</a>', hybrid_get_textdomain() ), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
 
		);

		return $messages;
  
	}
	
	// Customize and move featured image box to main column
	
	add_action( 'do_meta_boxes', 'sl_image_box' );
	
	function sl_image_box() {
		global $sl_settings;
		
		$title = __( 'Slide Image', hybrid_get_textdomain() ) . ' (' . $sl_settings['image_width'] . 'x' . $sl_settings['image_height'] . ')';
	
		remove_meta_box( 'postimagediv', 'slide', 'side' );
	
		add_meta_box( 'postimagediv', $title, 'post_thumbnail_meta_box', 'slide', 'normal', 'high' );
	
	}
	
	// Remove permalink metabox
	
	add_action( 'admin_menu', 'sl_remove_permalink_meta_box' );

	function sl_remove_permalink_meta_box() {
	
		remove_meta_box( 'slugdiv', 'slide', 'core' );
	
	}
		
	// Adds meta box for Slide URL
	
	add_action( 'admin_menu', 'sl_create_url_meta_box' );

	global $sl_new_meta_box;
	$sl_new_meta_box =

		array(
		
			'slide_url' => array(
			
				'name' => 'slide_url',
				'std'  => ''				
			)

		);

	function sl_new_meta_box() {
	
		global $post, $sl_new_meta_box;

		foreach ( $sl_new_meta_box as $sl_meta_box ) {

			$sl_meta_box_value = get_post_meta( $post->ID, $sl_meta_box['name'].'_value', true );  

			if( $sl_meta_box_value == "" ) $sl_meta_box_value = $sl_meta_box['std'];

			echo "<input type='hidden' name='" . $sl_meta_box['name'] . "_noncename' id='" . $sl_meta_box['name'] . " _noncename' value='" . wp_create_nonce( plugin_basename(__FILE__) ) . "' />";

			echo "<input type='text' name='" . $sl_meta_box['name'] . "_value' value='" . $sl_meta_box_value . "' size='55' /><br />";

			echo "<p>" . __('Add the URL this slide should link to.',hybrid_get_textdomain()) . "</p>";

		}

	}

	function sl_create_url_meta_box() {
	
		global $theme_name;

		if ( function_exists('add_meta_box') ) {

			add_meta_box( 'cvslides-url-box', __('Slide Link',hybrid_get_textdomain()), 'sl_new_meta_box', 'slide', 'normal', 'low' );

		}

	}
	
	// Save and retrieve the Slide URL data
	
	add_action( 'save_post', 'sl_save_postdata' );

	function sl_save_postdata( $post_id ) {

		global $post, $sl_new_meta_box;

		foreach( $sl_new_meta_box as $sl_meta_box ) {

			if ( !isset( $_POST[$sl_meta_box['name'].'_noncename']  ) || !wp_verify_nonce( $_POST[$sl_meta_box['name'].'_noncename'], plugin_basename(__FILE__) ) ) {

				return $post_id;

			}

			if ( 'page' == $_POST['post_type'] ) {

				if( !current_user_can( 'edit_page', $post_id ) )

				return $post_id;

			}
			
			else {
			
				if( !current_user_can( 'edit_post', $post_id ) )

				return $post_id;

			}

			$data = $_POST[$sl_meta_box['name'].'_value'];

			if ( get_post_meta( $post_id, $sl_meta_box['name'].'_value' ) == "" ) {
			
				add_post_meta( $post_id, $sl_meta_box['name'].'_value', $data, true );
			
			}
			
			elseif ( $data != get_post_meta( $post_id, $sl_meta_box['name'].'_value', true ) ) {

				update_post_meta( $post_id, $sl_meta_box['name'].'_value', $data );
			
			}

			elseif ( $data == "" ) {

				delete_post_meta( $post_id, $sl_meta_box['name'].'_value', get_post_meta( $post_id, $sl_meta_box['name'].'_value', true ) );
			
			}
			
		}

	}
	
	// Adds slide image and link to slides column view
	
	add_filter( 'manage_edit-slide_columns', 'sl_edit_columns' );
 
	function sl_edit_columns( $columns ) {
	
		$columns = array(
		
			'cb'         => '<input type="checkbox" />',
			'slide'      => __( 'Slide Image', hybrid_get_textdomain() ),
			'title'      => __( 'Slide Title', hybrid_get_textdomain() ),
			'slide-link' => __( 'Slide Link', hybrid_get_textdomain() ),
			'date'       => __( 'Date', hybrid_get_textdomain() )

		);
 
		return $columns;
  
	}
	
	add_action( 'manage_posts_custom_column', 'sl_custom_columns' );
	
	function sl_custom_columns( $column ) {
	
		global $post;
 
		switch ( $column ) {
		
			case 'slide' :
			
				echo the_post_thumbnail('featured-slide-thumb');
			
			break;
			
			case 'slide-link' :
			
				if ( get_post_meta($post->ID, "slide_url_value", $single = true) != "" ) {
				
					echo "<a href='" . get_post_meta($post->ID, "slide_url_value", $single = true) . "'>" . get_post_meta($post->ID, "slide_url_value", $single = true) . "</a>";
			
				}  
			
				else {
				
					_e('No Link', hybrid_get_textdomain());
			
				}
			
			break;

		}
		
	}
	
	//display contextual help for slides
	
	add_action( 'contextual_help', 'sl_add_help_text', 10, 3 );

	function sl_add_help_text($contextual_help, $screen_id, $screen) { 
	
		if ('slide' == $screen->id ) {
		
			$contextual_help =
			'<h3>' . __( 'Add New Slide', hybrid_get_textdomain() ) . '</h3>' .
			'<p>'  . __( '<strong>Title:</strong> Each slide needs a title in order to be published.', hybrid_get_textdomain() ) . '</p>' .
			'<p>'  . __( '<strong>Slide Image:</strong> To add an image to a slide, click the <strong>Set featured image</strong> link. Upload an image, or browse the media library for one, click the <strong>Use as featured image</strong> link to add the image and then close the media uploader. The Slide Image metabox should now have a thumbnail image.', hybrid_get_textdomain() ) . '</p>' .
			'<p>'  . __( '<strong>Slide Link:</strong> Add the full URL to the Slide Link metabox, such as <em>http://colorvila.com/</em> (Optional)', hybrid_get_textdomain() ) . '</p>' .
			'<p>'  . __( '<strong>Slideshows:</strong> A slide can be added to a slideshow by selecting the slideshow from the Slideshows metabox.', hybrid_get_textdomain() ) . '</p>' .
			'<p>'  . __( "<strong>Slide Order:</strong> Slides are sorted chronologically, edit the slide's published date to change the order of the slide.", "cv-slides" ) . '</p>' .
			'<h3>' . __( 'For more information', hybrid_get_textdomain() ) . '</h3>' .
			'<p>'  . __( '<a href="http://colorvila.com/forum" target="_blank">Support Forum</a>', hybrid_get_textdomain() ) . '</p>';
			
		} elseif ( 'edit-slide' == $screen->id ) {
		
			$contextual_help =
			
			'<h3>' . __( 'Slides', hybrid_get_textdomain() ) . '</h3>' .
			'<p>'  . __( 'Choose a slide to edit, or add a new slide.', hybrid_get_textdomain() ) . '</p>' .
			'<h3>' . __( 'For more information', hybrid_get_textdomain() ) . '</h3>' .
			'<p>'  . __( '<a href="http://colorvila.com/forum" target="_blank">Support Forum</a>', hybrid_get_textdomain() ) . '</p>';
			
		} elseif ( 'edit-slideshow' == $screen->id ) {
		
			$contextual_help =
			
			'<h3>' . __( 'Slideshows', hybrid_get_textdomain() ) . '</h3>' .
			'<p>'  . __( 'Slides can be organized into slideshows, just as posts can be organized into categories.', hybrid_get_textdomain() ) . '</p>' .
			'<p>'  . __( '<strong>Add New Slideshow:</strong> Name the slideshow, specify a Slug or one will be generated from the name, skip the Parent and Description and click <strong>Add New Slideshow</strong>.', hybrid_get_textdomain() ) . '</p>' .
			'<p>'  . __( '<strong>Add Slide to Slideshow:</strong> Edit a slide and select the slideshow in the Slideshows metabox.', hybrid_get_textdomain() ) . '</p>' .
			'<h3>' . __( 'For more information', hybrid_get_textdomain() ) . '</h3>' .
			'<p>'  . __( '<a href="http://colorvila.com/forum" target="_blank">Support Forum</a>', hybrid_get_textdomain() ) . '</p>';
			
		} 
		
		return $contextual_help;

	}

	
	
	add_filter( 'option_page_capability_sl_options', 'sl_options_cap' );
	
	function sl_options_cap( $capability ) {
	
		if ( function_exists( 'members_get_capabilities' ) ) {
	
			return 'sl_manage_options';
		
		} else {
	
			return 'manage_options';
		
		}
		
	}
		
	
	// Add custom slide capabilities for the Members plugin
	
	add_action( 'admin_init', 'sl_members_capabilities' );

	function sl_members_capabilities() {
	
		if ( function_exists( 'members_get_capabilities' ) ) {
	
			add_filter( 'members_get_capabilities', 'sl_add_members_caps' );
		
		}
	
	}

	function sl_add_members_caps( $caps ) {
	
		$caps[] = 'sl_manage_options';
		$caps[] = 'sl_edit_slide';
		$caps[] = 'sl_edit_slides';
		$caps[] = 'sl_edit_others_slides';
		$caps[] = 'sl_publish_slides';
		$caps[] = 'sl_read_slides';
		$caps[] = 'sl_read_private_slides';
		$caps[] = 'sl_delete_slide';
		$caps[] = 'sl_manage_slideshows';
		
		return $caps;
		
	}
	
	
	// Adds CSS for the Slides admin pages
	
	add_action( 'admin_enqueue_scripts', 'sl_admin_css' );

	function sl_admin_css() {
		
		global $post_type;
				
		if ( ( isset( $_GET['post_type'] ) && $_GET['post_type'] == 'slide' ) || ( isset( $post_type ) && $post_type == 'slide' ) ) {
	
			wp_enqueue_style( 'cv-slides-admin', CVINCLUDES_SLIDES_URI . 'css/cv-slides-admin.css', array(), '1.0' );
	
		}
		
	}

?>